// m is a Google Fusion Table
function Correlate(m, colX, colY) {
    var mlen = m.getNumberOfRows();
    var lcm = 0;
    var x = 0;
    var y = 0;
    var xy = 0;
    var xx = 0;
    var yy = 0;

    for(var j = 0; j < mlen; j++) {
        var mx = m.getValue(j, colX);
        var my = m.getValue(j, colY);

        x += mx;
        y += my;
        xx += mx*mx;
        yy += my*my;
        xy += mx*my;
    }
    var nu = (mlen*xy-x*y);
    var sqt = (mlen*xx-x*x)*(mlen*yy-y*y);
    var de = Math.sqrt(sqt);
    var corr = nu/de;
    corr = Math.round(corr*10000)/10000;
    return corr;
}


function GetCorrelateText(c) {
  var t = "";

  if (0.9 <= c) {
    t = "very highly correlated";
  } else if (0.7 <= c ) {
    t = "highly correlated";
  } else if (0.5 <= c ) {
    t = "moderately correlated";
  } else if (0.3 <= c ) {
    t = "weakly correlated";
  } else if (-0.3 < c ) {
    t = "not correlated";
  } else if (-0.5 < c ) {
    t = "weakly inverse correlated";
  } else if (-0.7 < c ) {
    t = "moderately inverse correlated";
  } else if (- 0.9 < c ) {
    t = "highly inverse correlated";
  } else {
    t = "very highly inverse correlated";
  }

  return t;
}


